package com.yhm.myfootbollshow.demos.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yhm.myfootbollshow.demos.base.ResVo;
import com.yhm.myfootbollshow.demos.entity.po.Order;
import com.yhm.myfootbollshow.demos.entity.po.OrderItem;
import com.yhm.myfootbollshow.demos.mapper.OrderItemMapper;
import com.yhm.myfootbollshow.demos.mapper.OrderMapper;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: yhm
 * @time: 2025/4/15 17:55
 */
@RestController
@RequestMapping("yhm/order")
public class OrderController {

    @Resource
    OrderMapper orderMapper;
    @Resource
    OrderItemMapper orderItemMapper;

    @PostMapping("user")
    public ResVo<List<Order>> listUser(Long userid) {
        // 判断是否为空
        if (userid == null) {
            return ResVo.error(null);
        }
        // 根据用户id查询订单列表
        List<Order> orders = orderMapper.selectList(new LambdaQueryWrapper<Order>()
                .eq(Order::getUserId, userid));
        return ResVo.ok(orders);
    }

    @PostMapping("merchant")
    public ResVo<List<Order>> listMerchant(Long merchantid) {
        // 判断是否为空
        if (merchantid == null) {
            return ResVo.error(null);
        }
        // 根据商家id查询订单详情列表
        List<OrderItem> orderItems = orderItemMapper.selectList(new LambdaQueryWrapper<OrderItem>()
                .select(OrderItem::getOrderId)
                .eq(OrderItem::getMerchantId, merchantid));
        ArrayList<Order> orders = new ArrayList<>();
        // 遍历商家相关订单详情 获取订单信息
        orderItems.forEach(orderItem -> {
            Order order = orderMapper.selectById(orderItem.getOrderId());
            orders.add(order);
        });

        return ResVo.ok(orders);
    }

    @PostMapping("list")
    public ResVo<List<Order>> list(Order order) {
        List<Order> orders = orderMapper.selectList(new LambdaQueryWrapper<Order>()
                .eq(order.getUserId() != null, Order::getUserId, order.getUserId())
                .eq(order.getStatus() != null, Order::getStatus, order.getStatus())
                .eq(order.getMerchantId() != null, Order::getMerchantId, order.getMerchantId()));
        return ResVo.ok(orders);
    }
}
